package com.hst.capacity.application.biz.impl;


import com.alibaba.fastjson2.JSON;
import com.hst.capacity.application.biz.AccountDriverBiz;
import com.hst.capacity.domain.model.entity.account.*;
import com.hst.capacity.domain.model.request.account.*;
import com.hst.capacity.domain.model.response.PagingResultVO;
import com.hst.capacity.domain.model.response.account.*;
import com.hst.capacity.infra.persistence.jpa.account.*;
import com.hst.capacity.infra.persistence.mybatis.AccountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AccountDriverBizImpl implements AccountDriverBiz {

    @Autowired
    private SpDriverAccountRepository spAccountRepository;
    @Autowired
    private SpecialDriverAccountDetailRepository spAccountDetailRepository;

    @Autowired
    private IcDriverAccountRepository icAccountRepository;
    @Autowired
    private IcDriverAccountDetailRepository icAccountDetailRepository;

    @Autowired
    private ExDriverAccountRepository exAccountRepository;
    @Autowired
    private ExDriverAccountDetailRepository exAccountDetailRepository;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public PagingResultVO<DriverAccountDetailVO> searchDriverAccountDetail(AccountDriverParam param) {
        switch (param.getServiceType()) {
            case 9://快车
                ExDriverAccountEntity exEntity = exAccountRepository.findByOwnerId(param.getIdDriver());
                param.setIdAccount(exEntity.getIdAccount());

            case 1://城际
                IcDriverAccountEntity icEntity = icAccountRepository.findByOwnerId(param.getIdDriver());
                param.setIdAccount(icEntity.getIdAccount());

            case 0://专车
                SpDriverAccountEntity spEntity = spAccountRepository.findByOwnerId(param.getIdDriver());
                param.setIdAccount(spEntity.getIdAccount());
        }

        log.info("AccountDriverParam：" + JSON.toJSONString(param));
        return new PagingResultVO<>(accountMapper.driverAccountDetailSearchTotal(param), accountMapper.driverAccountDetailSearch(param));
    }

    @Override
    public PagingResultVO<DriverAccountVO> searchDriverAccountList(AccountSearchParam param) {
        log.info("AccountSearchParam：" + JSON.toJSONString(param));
        switch (param.getServiceType()) {
            case 9:
                Page<DriverAccountVO> pageEx = exAccountRepository.findByAccountSearchParam(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
                return new PagingResultVO<>(pageEx.getTotalElements(), pageEx.getContent());

            case 1://乘客发单
                Page<DriverAccountVO> pageIc = icAccountRepository.findByAccountSearchParam(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
                return new PagingResultVO<>(pageIc.getTotalElements(), pageIc.getContent());

            case 0://乘客发单
                Page<DriverAccountVO> pageSp = spAccountRepository.findByAccountSearchParam(param.getIdFranchisee(), PageRequest.of(param.getPageIndex() - 1, param.getPageSize()));
                return new PagingResultVO<>(pageSp.getTotalElements(), pageSp.getContent());
        }

        return null;
    }

}
